package com.yanqu.road.server.channelserver.manager.activity.washepk;

import com.yanqu.road.dao.impl.channelcenter.ChannelActivityDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.area.wase.ChannelWashePkUserData;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.entity.activity.washe.data.WasheUserMatchData;
import com.yanqu.road.entity.activity.washe.game.WasheUserGameData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.washe.eWasheModeType;
import com.yanqu.road.entity.enums.washe.eWasheOperateType;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.pb.cross3.activity.AreaWashePkProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityInfoMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameModel;
import com.yanqu.road.server.channelserver.manager.activity.washe.ChannelWasheActivity;
import com.yanqu.road.server.channelserver.manager.activity.washe.msg.WasheNoticeHelper;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelWashePkMgr extends TempMgr {

    static Map<Integer, ChannelWashPkActivity> activityMap = new ConcurrentHashMap<>();

    public static ChannelWashPkActivity getActivity(int activityId) {
        if (!activityMap.containsKey(activityId)) {
            synchronized (activityMap) {
                if (!activityMap.containsKey(activityId)) {
                    ActivityInfo activityInfo = ChannelActivityInfoMgr.getActivityInfo(activityId);
                    if(activityInfo != null){
                        ChannelWashPkActivity activity = new ChannelWashPkActivity(activityInfo);
                        activity.setAllAreaRank(true);
                        if(activity.loadData()){
                            activityMap.put(activityId, activity);
                            ChannelEnterNameMgr.putEnterNameActivity(activityId, activity);
                            ChannelEnterNameMgr.setActivityInfo(activityInfo);
                        }
                    }else {
                        getLogger().info("ChannelWashePkMgr activity {} not found", activityId);
                    }
                }
            }
        }
        return activityMap.get(activityId);
    }

    public static ChannelWashPkActivity getActivityIfNull(int activityId) {
        return activityMap.get(activityId);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        //加载数据
        Map<Integer, ActivityInfo> allActivityMap = new ChannelActivityDaoImpl().getAllActivityInfoMap(ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
        for (Map.Entry<Integer, ActivityInfo> infoEntry : allActivityMap.entrySet()) {
            ActivityInfo activityInfo = infoEntry.getValue();
            if(activityInfo.getType() == eActivityType.AreaWashePkActivity.getValue()){
                if(ChannelActivityInfoMgr.activityInShowTime(activityInfo)){
                    ChannelActivityInfoMgr.getActivityInfoMap().put(activityInfo.getActivityId(), activityInfo);
                    getActivity(activityInfo.getActivityId());
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {

        for (Map.Entry<Integer, ChannelWashPkActivity> activityEntry : activityMap.entrySet()) {
            activityEntry.getValue().save();
        }

        //remove
        long now = System.currentTimeMillis();
        for (Map.Entry<Integer, ChannelWashPkActivity> entry : activityMap.entrySet()) {
            ChannelWashPkActivity activity = entry.getValue();
            ActivityInfo activityInfo = activity.getActivityInfo();
            if (now > activityInfo.getEndShowTime() * 1000 + DateHelper.DAY_MILLIONS) {
                activityMap.remove(entry.getKey());
                getLogger().debug("remove expire wash pk activity {}.", entry.getKey());
            }
        }

        return true;
    }


    public static ChannelWashPkActivity getInTimeActivityInfo(int channelId, long serverId){
        long now = System.currentTimeMillis();
        for (Map.Entry<Integer, ChannelWashPkActivity> pkActivityEntry : activityMap.entrySet()) {
            ChannelWashPkActivity activity = pkActivityEntry.getValue();
            if(now <= activity.getActivityInfo().getEndShowTime() * 1000){
                if(activity.getActivityInfo().getParam6().contains(String.valueOf(channelId)) || activity.getActivityInfo().getServerIdList().contains(serverId)){
                    return activity;
                }
            }
        }
        return null;
    }

    public static ChannelWashPkActivity handUserScore(WasheUserGameData player, boolean win, WasheProto.WasheGameOverMsg.Builder overBuilder, WasheNoticeHelper noticeHelper, ChannelWasheActivity washeActivity){
        WasheUserMatchData userMatchData = player.getUserMatchData();
        ChannelWashPkActivity activity = getInTimeActivityInfo(userMatchData.getChannelId(), userMatchData.getServerId());
        int scoreAdd = 0;
        if(activity != null){
            //是否报名
            UserEnterName enterName = ChannelEnterNameMgr.getUserEnterName(activity.getActivityInfo().getActivityId(), userMatchData.getUserId());
            if(enterName != null){
                ChannelWashePkUserData pkUserData = activity.getChannelUserData(userMatchData.getUserId(), enterName.getUserBaseInfo());
                //积分
                EnterNameModel enterNameModel = ChannelEnterNameMgr.getEnterNameModel(activity.getActivityInfo().getActivityId());
                if(enterNameModel != null){
                    if(win){
                        pkUserData.setWinTimes(1 + pkUserData.getWinTimes());
                        if(pkUserData.getWinTimes() > enterNameModel.getWinScoreList().size()){
                            scoreAdd = enterNameModel.getWinScoreList().get(enterNameModel.getWinScoreList().size() - 1);
                        }else {
                            scoreAdd = enterNameModel.getWinScoreList().get(pkUserData.getWinTimes() - 1);
                        }
                    }else {
                        pkUserData.setWinTimes(0);
                        scoreAdd = enterNameModel.getLoseScore();
                    }
                    if(pkUserData.getWinTimes() > pkUserData.getMaxWinTimes()){
                        pkUserData.setMaxWinTimes(pkUserData.getWinTimes());
                    }
                    pkUserData.setScore(pkUserData.getScore() + scoreAdd);
                    pkUserData.setLastUpdateTime(System.currentTimeMillis());
                    //该活动勋章就是分数
                    pkUserData.setBadge(pkUserData.getScore());
                    pkUserData.setLastUpdateTime2(System.currentTimeMillis());

                    overBuilder.setZoneId(enterName.getZoneId());
                }
                overBuilder.setWinTimes(pkUserData.getWinTimes());
                overBuilder.setScoreChange(scoreAdd);
            }
            overBuilder.setActivityId(activity.getActivityInfo().getActivityId());
        }


        //给客户端
        WasheProto.WasheSyncNoticeRespMsg.Builder clientMsg = WasheProto.WasheSyncNoticeRespMsg.newBuilder();
        clientMsg.setType(eWasheOperateType.GameOver.getValue());
        overBuilder.setReward(activity.getConfig().getScoreItemId() + "=" + scoreAdd);
        clientMsg.setOver(overBuilder);
        washeActivity.sendNotice(clientMsg, player.getUserMatchData(), eWasheModeType.AreaPk.getValue(), noticeHelper);

        return activity;
    }

    public static AreaWashePkProto.AreaWashePkGetUserDataResp.Builder getUserBuilder(ChannelWashePkUserData userData) {
        AreaWashePkProto.AreaWashePkGetUserDataResp.Builder clientMsg = AreaWashePkProto.AreaWashePkGetUserDataResp.newBuilder();
        clientMsg.setRet(0);
        AreaWashePkProto.AreaWashePkUserDataTemp.Builder builder = AreaWashePkProto.AreaWashePkUserDataTemp.newBuilder();
        builder.setScore(userData.getScore());
        builder.setContinueWinTimes(userData.getWinTimes());
        builder.setMaxContinueWinTimes(userData.getMaxWinTimes());

        ChannelWashPkActivity activity = getActivityIfNull(userData.getActivityId());
        if(activity != null){
            builder.addAllTodaySceneMode(activity.getSceneList());
        }
        clientMsg.setUserData(builder);
        return clientMsg;
    }

    public static Map<Integer, ChannelWashPkActivity> getActivityMap() {
        return activityMap;
    }
}
